package com.zhl.scheduling.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhl.scheduling.mapper.DepartmentMapper;
import com.zhl.scheduling.mapper.UserMapper;
import com.zhl.scheduling.model.bo.UsersListQueryBO;
import com.zhl.scheduling.model.entity.Department;
import com.zhl.scheduling.model.entity.User;
import com.zhl.scheduling.model.vo.ListVO;
import com.zhl.scheduling.model.vo.ResStatus;
import com.zhl.scheduling.model.vo.ResultVO;
import com.zhl.scheduling.service.SystemUsersService;
import com.zhl.scheduling.utils.MD5Utils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author: ZhangLu In DLUT
 * @since: 1.0.0
 * @Description: 系统设置接口的实现类
 */
@Service
public class SystemUsersServiceImpl implements SystemUsersService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private DepartmentMapper departmentMapper;

    /**
     * 根据客户端封装的用户查询条件，查询用户
     * @param usersListQueryBO 客户端封装的查询工件实体类
     * @return ResultVO 用作封装数据给前端的数据
     */
    @Override
    public ResultVO getList(UsersListQueryBO usersListQueryBO) {

        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();

        if (usersListQueryBO.getUserId() != null && !usersListQueryBO.getUserId().equals("")) {
            criteria.andEqualTo("userId", usersListQueryBO.getUserId());
        }
        if (usersListQueryBO.getUserIdCard() != null && !usersListQueryBO.getUserIdCard().equals("")) {
            criteria.andEqualTo("userIdCard", usersListQueryBO.getUserIdCard());
        }
        if (usersListQueryBO.getUsername() != null && !usersListQueryBO.getUsername().equals("")) {
            criteria.andLike("username", "%"+usersListQueryBO.getUsername()+"%");
        }
        if(usersListQueryBO.getUserDepartmentId() != null && !usersListQueryBO.getUserDepartmentId().equals("")){
            criteria.andEqualTo("userDepartmentId", usersListQueryBO.getUserDepartmentId());
        }
        if (usersListQueryBO.getUserStatus() != null && !usersListQueryBO.getUserStatus().equals("")) {
            criteria.andEqualTo("userStatus", usersListQueryBO.getUserStatus());
        }
        if (usersListQueryBO.getUserRole() != null && !usersListQueryBO.getUserRole().equals("")) {
            criteria.andEqualTo("userRole", usersListQueryBO.getUserRole());
        }
        if (usersListQueryBO.getOrderBy() != null && !usersListQueryBO.getOrderBy().equals("") ) {
            Example.OrderBy orderBy = example.orderBy(usersListQueryBO.getOrderBy().split(" ")[0]);
            if (usersListQueryBO.getOrderBy().split(" ")[1].equals("asc")){
                orderBy.asc();
            } else {
                orderBy.desc();
            }
        }
        Page<User> list = PageHelper.startPage(usersListQueryBO.getPage(), usersListQueryBO.getLimit()).doSelectPage(() -> userMapper.selectByExample(example));
        if (list != null) {
            ListVO listVO = new ListVO();
            listVO.setItems(list);
            listVO.setTotal(PageHelper.count(() -> userMapper.selectByExample(example)));
            return new ResultVO(ResStatus.OK,"获取列表成功", listVO);
        }
        return new ResultVO(ResStatus.NO,"获取列表失败", null);
    }

    /**
     * 根据客户端提供的部门信息进行添加
     * @param department 客户端封装的用户部门实体类
     * @return ResultVO 用作封装数据给前端的数据
     */
    @Override
    public ResultVO addDepartment(Department department) {
        int insertNum = departmentMapper.insert(department);
        if (insertNum > 0) {
            return new ResultVO(ResStatus.OK,"新增部门成功", null);
        }else {
            return new ResultVO(ResStatus.NO,"新增部门失败", null);
        }
    }

    /**
     * 根据客户端提供的用户信息来更新用户
     * @param user 客户端封装的用户实体类
     * @return ResultVO 用作封装数据给前端的数据
     */
    @Override
    public ResultVO updateUser(User user) {
        user.setUpdatedTime(new Date());
        String md5 = MD5Utils.md5(user.getPassword());
        user.setPassword(md5);
        int updateNum = userMapper.updateByPrimaryKey(user);
        if (updateNum > 0) {
            return new ResultVO(ResStatus.OK,"修改员工信息成功", null);
        }else {
            return new ResultVO(ResStatus.NO,"修改员工信息失败", null);
        }
    }

    /**
     * 根据客户端提供的用户信息来添加用户
     * @param user 客户端封装的用户实体类
     * @return ResultVO 用作封装数据给前端的数据
     */
    @Override
    public ResultVO addUser(User user) {
        user.setUpdatedTime(new Date());
        user.setCreatedTime(new Date());
        user.setPassword(MD5Utils.md5(user.getPassword()));
        int insertNum = userMapper.insert(user);
        if (insertNum > 0) {
            return new ResultVO(ResStatus.OK,"增加员工信息成功", null);
        }else {
            return new ResultVO(ResStatus.NO,"增加员工信息失败", null);
        }
    }
}
